﻿using Microsoft.EntityFrameworkCore;
using Super.Admin.EFCore;
using Super.Admin.Entities;
using Super.Admin.IServices;
using Super.Admin.Utils;

namespace Super.Admin.Services
{
    /// <summary>
    /// 用户操作日志服务实现类
    /// </summary>
    public class UserOperateLogService : BaseService, IUserOperateLogService
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context">操作的数据库上下文</param>
        public UserOperateLogService(SuperDataBaseDbContext context) : base(context) { }
        /// <summary>
        /// 查询用户操作日志
        /// </summary>
        /// <param name="operateType">操作类型</param>
        /// <param name="operateUserId">操作人id</param>
        /// <returns>用户操作日志实体集合</returns>
        public IReadOnlyCollection<UserOperateLogEntity> GetUserOperateLogs(int operateType, int operateUserId)
        {
            return Query<UserOperateLogEntity>(u => u.OperateType == operateType && u.CreateUserId == operateUserId).OrderByDescending(u => u.OperateTime).Take(30).ToList();
        }
        /// <summary>
        /// 查询用户操作日志
        /// </summary>
        /// <param name="message">信息</param>
        /// <param name="operateUserId">操作人id</param>
        /// <param name="permissionKey">权限标识</param>
        /// <param name="operateType">操作类型</param>
        /// <param name="beginTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="pageSize">一页数量</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="totalCount">总数</param>
        /// <returns>用户操作日志实体集合</returns>
        public IReadOnlyCollection<UserOperateLogEntity> GetUserOperateLogs(string message, int operateUserId, string permissionKey, int operateType, string beginTime, string endTime, int pageSize, int pageIndex, out int totalCount)
        {
            PageResult<UserOperateLogEntity> result = QueryPage<UserOperateLogEntity, string>(u =>
                           (string.IsNullOrEmpty(message) || u.Message.Contains(message)) &&
                           (operateUserId == -1 || u.CreateUserId == operateUserId) &&
                           (string.IsNullOrEmpty(permissionKey) || u.PermissionKey.Contains(permissionKey)) &&
                           (operateType == -1 || u.OperateType == operateType) &&
                           (string.IsNullOrEmpty(beginTime) || u.OperateTime.CompareTo(beginTime) > 0) &&
                           (string.IsNullOrEmpty(endTime) || u.OperateTime.CompareTo(endTime) < 0),
                           pageSize,
                           pageIndex,
                           x => x.OperateTime,
                           false,
                           q => q.Include(u => u.CreateUser));
            totalCount = result.TotalCount;
            return result.DataList;
        }
        /// <summary>
        /// 创建用户操作日志
        /// </summary>
        /// <param name="operateUserId">操作人id</param>
        /// <param name="message">信息</param>
        /// <param name="permissionKey">权限标识</param>
        /// <param name="operateType">操作类型</param>
        public void CreateUserOperateLog(int operateUserId, string message, string permissionKey, int operateType = 1)
        {
            UserOperateLogEntity entity = new UserOperateLogEntity
            {
                CreateUserId = operateUserId,
                OperateType = operateType,
                Message = message,
                IpAddress = IpAddressHelper.Instance.GetLocalIPv4Address(),
                OperateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                PermissionKey = permissionKey,
            };
            Insert<UserOperateLogEntity>(entity);
            base.Commit(); // 提交事务
        }
    }
}
